package com.well.WellProject.Service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.well.WellProject.Dao.UserMapper;
import com.well.WellProject.Entity.UserInfo;
import com.well.WellProject.Service.MainService;
import com.well.WellProject.Tools.ResultUtil;

@Service
public class MainServiceImpl implements MainService {

	@Resource
	UserMapper userMapper;

	@Override
	public String register(String data) {
		try {
			Map<String, Object> paramMap = ResultUtil.getParamMap(data);
			boolean isExist = this.checkUserExist(paramMap);

			if (isExist) {
				return ResultUtil.outString(11, "用户已被注册");
			} else {
				userMapper.register(paramMap);
				return ResultUtil.outString();
			}

		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.outErrorString();
		}

	}

	@Override
	public boolean checkUserExist(Map<String, Object> map) {
		boolean isExist = false;
		Integer resultCount = userMapper.checkUserExist(map);
		if (resultCount > 0) {
			isExist = true;
		}
		return isExist;
	}

	@Override
	public String login(String data) {
		try {
			Map<String, Object> paramMap = ResultUtil.getParamMap(data);
			Map<String, Object> resultMap = userMapper.login(paramMap);
//			Map<String, Object> returnMap = new Map<String, Object>;
			if (resultMap != null) {
//				paramMap.put("Result", 0);
//				paramMap.put("Response", resultMap);
//				return ResultUtil.outString(paramMap);
				resultMap.remove("password");
				return ResultUtil.outString(paramMap, resultMap);
			}else {
				return ResultUtil.outString(-1, "用户名或者密码错误");
			}
			

		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.outErrorString();
		}
	}
	
	@Override
	public String getUserList(String data) {
		try {
			Map<String, Object> paramMap = ResultUtil.getParamMap(data);
			List<UserInfo> userList = userMapper.getUserList();			
			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("userList", userList);
			return ResultUtil.outString(paramMap, resultMap);
			
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.outErrorString();
		}
	}

}
